package com.gjy.spring.s5;

import org.springframework.context.support.GenericApplicationContext;

import java.io.IOException;

/**
 * @author 宫静雨
 * @version 1.0
 * @since 2023-12-19 11:04:11
 */
public class S5App {

    public static void main(String[] args) throws IOException {
        GenericApplicationContext context = new GenericApplicationContext();
        context.registerBean("config", Config.class);
        //context.registerBean(ConfigurationClassPostProcessor.class); // @ComponentScan @Bean @Import
        //context.registerBean(MapperScannerConfigurer.class, bd -> bd.getPropertyValues().add("basePackage", "com.gjy.spring.s5.mapper")); // @MapperScan
        /*@ComponentScan*/
//        ComponentScan annotation = AnnotationUtils.findAnnotation(Config.class, ComponentScan.class);
//        if (annotation != null) {
//            for (String bp : annotation.basePackages()) {
//                // classpath*:com/gjy/**/*.class
//                String s = "classpath*:" + bp.replace(".", "/") + "/**/*.class";
//                CachingMetadataReaderFactory factory = new CachingMetadataReaderFactory();
//                Resource[] resources = context.getResources(s);
//                AnnotationBeanNameGenerator generator = new AnnotationBeanNameGenerator();
//                for (Resource resource : resources) {
//                    MetadataReader reader = factory.getMetadataReader(resource);
//                    System.out.println("类名: " + reader.getClassMetadata().getClassName());
//                    //System.out.println("是否加了 @Component: " + reader.getAnnotationMetadata().hasAnnotation(Component.class.getName()));
//                    //System.out.println("是否加了 @Component 派生: " + reader.getAnnotationMetadata().hasMetaAnnotation(Component.class.getName()));
//                    AnnotationMetadata annotationMetadata = reader.getAnnotationMetadata();
//                    if (annotationMetadata.hasAnnotation(Component.class.getName()) ||
//                            annotationMetadata.hasMetaAnnotation(Component.class.getName())) {
//                        AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder.genericBeanDefinition(reader.getClassMetadata().getClassName())
//                                .getBeanDefinition();
//                        String beanName = generator.generateBeanName(beanDefinition, context.getDefaultListableBeanFactory());
//                        context.getDefaultListableBeanFactory().registerBeanDefinition(beanName, beanDefinition);
//                    }
//                }
//            }
//        }
//        context.registerBean("componentScanPostProcessor", ComponentScanPostProcessor.class);

        /*@Bean*/
        /*CachingMetadataReaderFactory factory = new CachingMetadataReaderFactory();
        MetadataReader reader = factory.getMetadataReader(new ClassPathResource("com/gjy/spring/s5/Config.class"));
        Set<MethodMetadata> methods = reader.getAnnotationMetadata().getAnnotatedMethods(Bean.class.getName());
        for (MethodMetadata method : methods) {
            String initMethod = method.getAnnotationAttributes(Bean.class.getName()).get("initMethod").toString();

            BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition();
            builder.setFactoryMethodOnBean(method.getMethodName(), "config");
            builder.setAutowireMode(AbstractBeanDefinition.AUTOWIRE_CONSTRUCTOR); // 设置自动注入
            if (initMethod.length() > 0) {
                builder.setInitMethodName(initMethod);
            }
            AbstractBeanDefinition bd = builder.getBeanDefinition();
            context.getDefaultListableBeanFactory().registerBeanDefinition(method.getMethodName(), bd);
        }*/
        context.registerBean(AtBeanPostProcessor.class);
        context.registerBean(MapperPostProcessor.class);
        context.refresh();
        for (String s : context.getBeanDefinitionNames()) {
            System.out.println(s);
        }
        context.close();
    }
}
